package com.ewhale.feitengguest.presenter.user.auth;

import com.ewhale.feitengguest.api.ApiHelper;
import com.ewhale.feitengguest.dto.LoginInfoDto;
import com.ewhale.feitengguest.view.auth.BindAccountView;
import com.orhanobut.hawk.Hawk;
import com.simga.library.base.IPresenter;
import com.simga.library.http.APIException;
import com.simga.library.utils.CheckUtil;

import java.util.HashMap;
import java.util.Map;

public class BindAccountPresenter extends IPresenter {
    private String USER_BIND = "app/account/bindPhone.json";
    private String SHOP_BIND = "app/shop/shopBindPhone.json";
    public void getCode(String sessionId, String registerPhone) {
        mView.showProLoading();
        request(1, ApiHelper.AUTH_API.getCode(sessionId, registerPhone, "3"), null);
    }

    public void bindPhone(int bindType, String phone, String code, String registerPwd, String inviteCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("phone", phone);
        params.put("phoneCode", code);
        params.put("password", registerPwd);
        params.put("repeatPassword", registerPwd);
        if (bindType == 1) {
            params.put("shopType", inviteCode.equals("个人") ? 1 : 2);
        }else{
            if (!CheckUtil.isNull(inviteCode)) {
                params.put("recommendCode", inviteCode);
            }
        }
        request(2, ApiHelper.AUTH_API.bindPhone(bindType == 1? SHOP_BIND: USER_BIND,params), null);
    }

    @Override
    public <T> void onNext(int postCode, T result, Object data) {
        super.onNext(postCode, result, data);
        switch (postCode) {
            case 1:
                ((BindAccountView) mView).getCodeSuccess();
                break;
            case 2:
                ((BindAccountView) mView).bindPhoneSuccess((LoginInfoDto) result);
                break;
        }
    }

    @Override
    public void otherException(int postCode, Object data, APIException e) {
        super.otherException(postCode, data, e);
        mView.showErrorMessage(postCode, e.getCode(), e.getDisplayMessage());
    }

}
